global data_folder "W:\intimate\data"
global log_folder "W:\intimate\dofiles\logs"
global result_folder "W:\intimate\results"


use  "W:\intimate\data\dvonly_all.dta", clear 
preserve 
	g suspect_events = 1
	collapse (sum) suspect_events, by(shnro)
	save  "W:\intimate\data\women_suspect", replace
	ren suspect_events spouse_suspect_events 
	ren shnro sphnro0
	save  "W:\intimate\data\male_suspect", replace
restore 

preserve 
	g victim_events = 1
	collapse (sum) victim_events, by(ao_shnro)
	ren ao_shnro shnro
	save  "W:\intimate\data\women_victim", replace
	ren victim_events spouse_victim_events 
	ren shnro sphnro0
	save  "W:\intimate\data\male_victim", replace
restore 

preserve 
	g couple_events = 1
	collapse (sum) couple_events, by(shnro ao_shnro)
	ren ao_shnro sphnro0
	save  "W:\intimate\data\women_sus_couple", replace
	ren couple_events spouse_couple_events 
	ren shnro tmp
	ren sphnro0 shnro
	ren tmp sphnro0
	save  "W:\intimate\data\male_sus_couple", replace
restore 

// Merge in whether DV or not 
use "W:\intimate\data\allwomen_data_clean", clear
merge m:1 shnro using  "W:\intimate\data\women_suspect"
drop if _merge==2
drop _merge

merge m:1 sphnro0 using  "W:\intimate\data\male_suspect"
drop if _merge==2
drop _merge

merge m:1 shnro using  "W:\intimate\data\women_victim"
drop if _merge==2
drop _merge

merge m:1 sphnro0 using  "W:\intimate\data\male_victim"
drop if _merge==2
drop _merge

merge m:1 shnro sphnro0 using  "W:\intimate\data\women_sus_couple"
drop if _merge==2
drop _merge

merge m:1 shnro sphnro0 using  "W:\intimate\data\male_sus_couple"
drop if _merge==2
drop _merge

// drop female suspects in any relationship 
drop if missing(suspect_events)==0
drop suspect_events couple_events 
ren spouse_couple_events dv_couple_events 
g dv_couple = 1 - missing(dv_couple_events)

// drop male victims in any relationship 
drop if missing(spouse_victim_events)==0
drop spouse_victim_events

ren spouse_suspect_events suspect_events
replace suspect_events=0 if missing(suspect_events)
replace victim_events=0 if missing(victim_events)
replace dv_couple_events=0 if missing(dv_couple_events)



* add in language and kids for men 
forvalues y=2006/2016{
use "W:\intimate\data\allwomen_data_clean_withDV", clear
keep if year_start_cohab==`y'
gen year=year_start_cohab

g woman_id = shnro
g man_id = sphnro0
drop shnro sphnro0

// add in men children
g shnro = man_id
local y_1 = `y'-1
merge m:1  shnro using  "$data_folder\fertility_`y_1'.dta", keepusing(nchild) keep(master match) nogen 
replace nchild=0 if nchild==. 
ren nchild spouse_pre_nchild

merge m:1 shnro using "D:\ready-made\FLEED_TOTAL\2016\fleed_kokonais_`y'.dta", keepusing(kieli) keep(master match) nogen 
ren kieli spouse_kieli

drop shnro 
g shnro = woman_id 
merge m:1 shnro using "D:\ready-made\FLEED_TOTAL\2016\fleed_kokonais_`y'.dta", keepusing(kieli) keep(master match) nogen 

drop shnro 
ren woman_id shnro
ren man_id sphnro0

tempfile cat_`y'
save `cat_`y'', replace 
}

clear 
forvalues y=2006/2016{
    append using `cat_`y''
}

save "W:\intimate\data\allwomen_data_clean_withDV", replace

// add in all violence indicators 
use "W:\intimate\data\violence_gender_indicator", clear
ren shnro sphnro0
save "W:\intimate\data\violence_gender_indicator", replace

use "W:\intimate\data\allwomen_data_clean_withDV", clear

merge m:1 sphnro0 using  "W:\intimate\data\violence_gender_indicator"
drop if _merge==2
drop _merge
drop fm_crime ff_crime min_yr_ff min_yr_fm
replace mf_crime = 0 if missing(mf_crime)
replace mm_crime = 0 if missing(mm_crime)
replace mf_crime = 1 if dv_couple==1
*replace min_yr_mf = year_start_cohab if mf_crime==1 & dv_couple==1 & min_yr_mf > 2020

save "W:\intimate\data\allwomen_data_clean_withAnyViolence", replace




